<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\User;

use App\Controller\AppBaseController;
use App\Model\Task;
use App\Model\Notify;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\WxApiAuth;

#[Controller(prefix: 'user')]
#[Middleware(WxApiAuth::class)]
class TaskController extends AppBaseController
{
    /**
     * 支部任务列表(党委委员)
     * @param int|null $party_id
     * @return object
     */
    #[RequestMapping(path:'taskList[/{party_id}]',methods: 'get')]
    public function taskList(int|null $party_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

//            if(!is_numeric($party_id) && $party_id==0){
//                return $this->reMsg->re(1,'success',['list' => [], 'next' => 0]);
//            }
            //支部任务只对党支部 管理员或者支部书记开放
//            if(!in_array($_user->user_type,[10])){
//                return $this->reMsg->re(1,'success',['list' => [], 'next' => 0]);
//            }

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $where['status'] = 1;

            if(is_numeric($party_id) && $party_id>0){
                $where['party_id'] = $party_id;
            }

            $list = Task::where($where)
                ->selectRaw('id,type,title,photo,created_at')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();


//            $list = Task::with(['joinStatus'=>function($query){
//                    $query->where(['module'=>1,'status'=>1])->selectRaw('id,relation_id,uid,file_list,status')->get();
//                }])->where($where)
//                ->selectRaw('id,type,title,photo,created_at')
//                ->orderByDesc('id')
//                ->offset($offset)
//                ->limit($limit)
//                ->get();


//         "join_status"：{
//             "id":"编号",
//			   "relation_id":"关联id",
//			   "uid":"用户id"
//		   }


            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>=$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 支部任务列表（新）
     * @return object
     */
    #[RequestMapping(path:'taskListV2',methods: 'get')]
    public function taskListV2(): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $where['uid'] = $_user->id;
            $where['module'] = 2;
            $where = [
                ['module','=',2],
                ['uid','=',$_user->id],
                ['relation_id','>',0],
            ];
            //支部任务
            $list = Notify::with(['task'=>function($query){
                    $query->selectRaw('id,type,title,created_at')->get();
                }])->where($where)
                ->selectRaw('id,module,relation_id,uid,status,created_at')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();
            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>=$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 支部任务详情
     * @param int $task_id
     * @return object
     */
    #[RequestMapping(path:'taskInfo/{task_id}',methods: 'get')]
    public function taskInfo(int $task_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

//            if(!in_array($_user->user_type,[8,9,10])) return $this->reMsg->re(10009);

//            $info = Task::with(['joinStatus'=>function($query){
//                    $query->where([
//                        'status'=>1,'module'=>2,'uid'=>$_user->id
//                    ])->selectRaw('id,relation_id,uid,file_list,status')->get();
//                }])->where('id',$task_id)
//                ->selectRaw('id,type,title,content,created_at')
//                ->first();
            $info = Task::where('id',$task_id)
                ->selectRaw('id,type,title,content,created_at')
                ->first();
            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 支部任务完成
     * @param int $task_id
     * @return object
     */
    #[RequestMapping(path: 'taskJoin/{task_id}',methods: 'post,get')]
    public function taskJoin(int $task_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];

            //判断信息
            $info = Task::where('id',$task_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);

            //验证权限
            if($_user->user_type==10) return $this->reMsg->re(10009);
            if(!in_array($_user->user_type,[8,9])) return $this->reMsg->re(10009);

            $file_list = $this->request->input('file_list',null);

            //文件任务
            if($info->type==2){
//                if(empty($file_list)){
//                    $file_list[0]['name'] = 'ceshi';
//                    $file_list[0]['url'] = 'https://www.baidu.com';
//                    $file_list[1]['name'] = 'ceshi2';
//                    $file_list[1]['url'] = 'https://www.baidu.com';
//                }
                if(empty($file_list)) return $this->reMsg->re(10007);
            }

            //支部任务模块
            $data['module'] = 2;  //支部任务
            $data['relation_id'] = $info->id;
            $data['uid'] = $_user->id;
            $joinInfo = Notify::where($data)->first();
            if(empty($joinInfo)) return $this->reMsg->re(10008);
            if($joinInfo->status==1) return $this->reMsg->re(10010,'任务已完成');
            if($info->type==2){
                $joinInfo->file_list = $file_list;
            }
            $joinInfo->status = 1;
            $joinInfo->save();

            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

}